package com.ruoyi.web.controller.gwc;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gwc.domain.TVehicleApplication;
import com.ruoyi.gwc.service.ITVehicleApplicationService;
import com.ruoyi.system.domain.DriverInfo;
import com.ruoyi.system.domain.VehicleManagement;
import com.ruoyi.system.service.IDriverInfoService;
import com.ruoyi.system.service.IVehicleManagementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gwc.domain.TDispatchVehicles;
import com.ruoyi.gwc.service.ITDispatchVehiclesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 调度车辆Controller
 * 
 * @author wp
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/gwc/vehicles")
public class TDispatchVehiclesController extends BaseController
{
    @Autowired
    private ITDispatchVehiclesService tDispatchVehiclesService;

    @Autowired
    private ITVehicleApplicationService tVehicleApplicationService;

    @Autowired
    private IDriverInfoService driverInfoService;
    @Autowired
    private IVehicleManagementService vehicleManagementService;

    /**
     * 查询调度车辆列表
     */
    @ApiOperation("查询调度车辆列表")
    @GetMapping("/list")
    public TableDataInfo list(TDispatchVehicles tDispatchVehicles)
    {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((tDispatchVehicles.getCompanyId()))) {
            tDispatchVehicles.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<TDispatchVehicles> list = tDispatchVehiclesService.selectTDispatchVehiclesList(tDispatchVehicles);
        return getDataTable(list);
    }

    /**
     * 导出调度车辆列表
     */
    @Log(title = "调度车辆", businessType = BusinessType.EXPORT)
    @ApiOperation("导出调度车辆列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDispatchVehicles tDispatchVehicles)
    {
        Long dispatchOrderStatus = tDispatchVehicles.getDispatchOrderStatus();
        LoginUser loginUser = getLoginUser();
        List<TVehicleApplication> list = new ArrayList<>();
        //调度状态（1：待调度，2：已调度）
        if(dispatchOrderStatus == 2 ) {
            TDispatchVehicles dispatchVehicles = new TDispatchVehicles();
            dispatchVehicles.setDispatchOrderStatus(dispatchOrderStatus);
            if (!loginUser.getUser().isAdmin()) {
                dispatchVehicles.setCreateBy(loginUser.getUser().getUserName());
            }
            list = tVehicleApplicationService.getScheduledVehicleList(dispatchVehicles);
        }else {
            TVehicleApplication vehicleApplication = new TVehicleApplication();
            vehicleApplication.setOrderStatus(1L);//订单状态（0：已申请，1：已审批，2已调度，3已接单，4已出车，5已还车，6撤销订单）
            list = tVehicleApplicationService.selectTVehicleApplicationList(vehicleApplication);
        }
        ExcelUtil<TVehicleApplication> util = new ExcelUtil<TVehicleApplication>(TVehicleApplication.class);
        util.hideColumn("remark");
        util.exportExcel(response, list, "调度车辆数据");
    }

    /**
     * 获取调度车辆详细信息
     */
    @ApiOperation("获取调度车辆详细信息")
    @GetMapping(value = "/{dispatchId}")
    public AjaxResult getInfo(@PathVariable("dispatchId") Long dispatchId)
    {
        return success(tDispatchVehiclesService.selectTDispatchVehiclesByDispatchId(dispatchId));
    }

    /**
     * 新增调度车辆
     */
    @Log(title = "调度车辆", businessType = BusinessType.INSERT)
    @ApiOperation("新增调度车辆")
    @PostMapping
    public AjaxResult add(@RequestBody List<TDispatchVehicles> tDispatchVehicleslist)
    {
        for (TDispatchVehicles tDispatchVehicles : tDispatchVehicleslist) {
            //订单状态（2已调度，3已接单，4已出车，5已还车）
            tDispatchVehicles.setDispatchOrderStatus(2L);
            if (tDispatchVehiclesService.insertTDispatchVehicles(tDispatchVehicles) > 0) {
                TVehicleApplication tVehicleApplication = new TVehicleApplication();
                tVehicleApplication.setApplyId(tDispatchVehicles.getApplyId());
                tVehicleApplication.setOrderStatus(2L);//2已调度
                tVehicleApplicationService.saveOrUpdate(tVehicleApplication);

                //更新车辆状态
                VehicleManagement vehicleManagement = new VehicleManagement();
                vehicleManagement.setId(tDispatchVehicles.getVehicleId());
                vehicleManagement.setVehicleTaskStatus(1);//车辆状态0空闲1任务中
                vehicleManagementService.updateById(vehicleManagement);

                //更新司机状态
                DriverInfo driverInfo = new DriverInfo();
                driverInfo.setId(tDispatchVehicles.getDriverId());
                driverInfo.setDriverTaskStatus(1);//司机状态0空闲1任务中
                driverInfoService.updateById(driverInfo);

            } else {
                return error("新增调度车辆失败");
            }
        }
        return success();
    }

    /**
     * 修改调度车辆
     */
    @Log(title = "调度车辆", businessType = BusinessType.UPDATE)
    @ApiOperation("修改调度车辆")
    @PutMapping
    public AjaxResult edit(@RequestBody List<TDispatchVehicles> tDispatchVehicleslist)
    {
        for (TDispatchVehicles tDispatchVehicles : tDispatchVehicleslist) {
            tDispatchVehiclesService.updateTDispatchVehicles(tDispatchVehicles);
        }
        return success();
    }

    /**
     * 删除调度车辆
     */
    @Log(title = "调度车辆", businessType = BusinessType.DELETE)
    @ApiOperation("删除调度车辆")
	@DeleteMapping("/{dispatchIds}")
    public AjaxResult remove(@PathVariable Long[] dispatchIds)
    {
        return toAjax(tDispatchVehiclesService.deleteTDispatchVehiclesByDispatchIds(dispatchIds));
    }

    @ApiOperation("查询调度用车审批单")
    @GetMapping("/getScheduledVehicleList")
    public TableDataInfo getScheduledVehicleList(@RequestParam Long dispatchOrderStatus,@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        startPage();
        LoginUser loginUser = getLoginUser();
        List<TVehicleApplication> list = new ArrayList<>();
        //调度状态（1：待调度，2：已调度）
        if(dispatchOrderStatus == 2 ) {
            TDispatchVehicles dispatchVehicles = new TDispatchVehicles();
            dispatchVehicles.setDispatchOrderStatus(dispatchOrderStatus);
            if (!loginUser.getUser().isAdmin()) {
                dispatchVehicles.setCreateBy(loginUser.getUser().getUserName());
                dispatchVehicles.setCompanyId(loginUser.getUser().getDeptId());
            }
            list = tVehicleApplicationService.getScheduledVehicleList(dispatchVehicles);
        }else {
            TVehicleApplication vehicleApplication = new TVehicleApplication();
            if (!loginUser.getUser().isAdmin()) {
                vehicleApplication.setCompanyId(loginUser.getUser().getDeptId());
            }
            vehicleApplication.setOrderStatus(1L);//订单状态（0：已申请，1：已审批，2已调度，3已接单，4已出车，5已还车，6撤销订单）
            list = tVehicleApplicationService.selectTVehicleApplicationList(vehicleApplication);
        }

        return getDataTable(list);
    }
}
